package com.cqupt.qualityevaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cqupt.qualityevaluation.dao.schoolhostcodeMapper;
import com.cqupt.qualityevaluation.pojo.schoolhostcode;
import com.cqupt.qualityevaluation.pojo.schoollocationtypecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/schoolhost")
public class SchoolHostController {
    @Autowired
    private schoolhostcodeMapper schoolhostcodeMapper;
    @GetMapping("/findAll")
    public List<schoolhostcode> findAll(){
        return schoolhostcodeMapper.selectList(null);
    }
    @PostMapping("/save")
    //通过RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),将JSON对象转换为java对象
    public Object save(@RequestBody schoolhostcode s){
        Map map=new HashMap<>();
        int result=0;
        try {
            result=schoolhostcodeMapper.insert(s);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","fail");
            map.put("msg","该学校举办者代码已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","添加成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    @DeleteMapping("/delete/{schoolhostcode}")
    public String delete(@PathVariable("schoolhostcode") String schoolhostcode){
        QueryWrapper<schoolhostcode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolhostcode",schoolhostcode);
        schoolhostcodeMapper.delete(queryWrapper);
        return "success";
    }
    @PutMapping("/update")
    public Object update(@RequestBody schoolhostcode st){
        Map map=new HashMap<>();
        int result=0;
        UpdateWrapper<schoolhostcode> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",st.getId());
        schoolhostcode schoolhostcode = new schoolhostcode();
        schoolhostcode.setSchoolhostcode(st.getSchoolhostcode());
        schoolhostcode.setSchoolhostname(st.getSchoolhostname());
        try{
            result=schoolhostcodeMapper.update(schoolhostcode,updateWrapper);
        }catch (org.springframework.dao.DuplicateKeyException e){
            map.put("code","fail");
            map.put("msg","该学校举办者代码已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","更新成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    @GetMapping("/selectone/{id}")
    public schoolhostcode findBySchoolHost(@PathVariable("id") String id){
        QueryWrapper<schoolhostcode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return schoolhostcodeMapper.selectOne(queryWrapper);
    }
    @GetMapping("/search/1/{schoolhostcode}")
    public List<schoolhostcode> findBySchoolHostCode(@PathVariable("schoolhostcode") String schoolhostcode){
        QueryWrapper<schoolhostcode> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoolhostcode",schoolhostcode);
        return schoolhostcodeMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/2/{schoolhostname}")
    public List<schoolhostcode> findBySchoolHostName(@PathVariable("schoolhostname") String schoolhostname){
        QueryWrapper<schoolhostcode> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoolhostname",schoolhostname);
        return schoolhostcodeMapper.selectList(queryWrapper);
    }
}
